from build_trie import *

def rank_file(modified_file_name, source_file_name):
    index_of_last_stop = modified_file_name.rindex('.')
    output_file_name = modified_file_name[:index_of_last_stop] + '_rank_added' + '.' + 'csv'
    root = build_tree_from_file(source_file_name)
    with open(modified_file_name, "r") as modified_file:
        with open(output_file_name, "w+") as output_file:
            for line in modified_file.readlines():
                line = line.strip() # 去掉空白符和换行符
                # 读取英文,中文,count部分,补足不全
                str_list = line.split(',')
                length = len(str_list)
                if(length == 1):
                    str_list.append("")
                    str_list.append("")
                    length = 3
                elif(length == 2):
                    str_list.append("")
                # 获取count并输出
                str_list[2] = str(get_count(str_list[0], root))
                joined = ','.join(str_list)
                output_file.write(joined)
                output_file.write("\n")

if(__name__ == "__main__"):
    source_file_name = "../words.csv"
    modified_file_name = "../unranked.csv"
    #modified_file_name = "./test.csv" #mother,,28084  明显有bug好吧
    rank_file(modified_file_name, source_file_name)
